/**
 * 后端接口返回的菜单数据转为前端数据格式
 * @param menus 菜单数据
 * @param breadcrumb 面包屑
 * @param level
 * @constructor
 */
export function CreatedMenuData(menus: Array<any>, breadcrumb: Array<string> = [], level: number = 0): Array<any> {
    const MenuData: Array<any> = []
    for (let menu of menus) {
        if (!menu?.meta?.show) {
            continue;//菜单不显示
        }
        let children: Array<any> = []
        if (menu?.children instanceof Array && menu?.children.length > 0) {
            children = CreatedMenuData(menu?.children, [menu.title], level + 1)
        }
        let row: any = {key: menu.name,pid:menu.pid, path: menu.path, type: "menu",icon:menu.meta?.icon?menu.meta?.icon:'Help'}
        if (children.length > 0) {//菜单设置了子菜单
            row.children = children
            row.label = menu.title
        } else if (menu.path.length > 0) {// 菜单没设置路由,说明是菜单组，但没有下级
            row.label = menu.title
        } else {
            continue;
        }
        MenuData.push(row)
    }
    return MenuData
}


/**
 * 获取需要注册的路由信息
 * @param menus
 * @param breadcrumb
 */
export function getRegisterRouter(menus: Array<any>, breadcrumb: Array<string> = []): Array<any> {
    const MenuData: Array<any> = []
    for (let menu of menus) {
        if (menu?.children instanceof Array && menu?.children.length > 0) {
            getRegisterRouter(menu?.children, [menu.title]).forEach(child => {
                MenuData.push(child)
            })
        }
        if (!menu.path) {//没有路径
            continue;//菜单不显示
        }
        let row: any = {
            key: menu.name,
            name: menu.name,
            path: menu.path,
            meta: {
                show:menu?.meta?.show,
                title: menu.title,//标题
                tourist: false,//游客是否可访问
                icon: menu?.meta?.icon,
                auth: [],//权限
                breadcrumb: [...breadcrumb, menu.title],//面包屑
            }
        }
        MenuData.push(row)
    }
    return MenuData
}